package com.luoy.table;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import java.io.IOException;

public class TableMapper extends Mapper<LongWritable, Text, Text, TableBean>{
	TableBean bean = new TableBean();
	Text k = new Text();
	
	@Override
	protected void map(LongWritable key, Text value, Context context)
			throws IOException, InterruptedException {
		
		// 1.获取输入文本类型
		FileSplit inputSplit = (FileSplit) context.getInputSplit();
		// 拿到切片名称
		String name = inputSplit.getPath().getName();
		// 2.获取输入数据
		String line = value.toString();
		
		if (name.startsWith("order")) {
			// 订单信息处理，切割数据
			String[] fields = line.split("\t");
			// 封装数据
			bean.setOrder_id(fields[0]);
			bean.setP_id(fields[1]);
			bean.setAmount(Integer.parseInt(fields[2]));
			bean.setPname("");
			bean.setFlag("0");
			
			// 设置k值
			k.set(fields[1]);
			
		}else {
			// 产品信息处理
			String[] fields = line.split("\t");
			
			// 封装数据
			bean.setOrder_id("");
			bean.setP_id(fields[0]);
			bean.setAmount(0);
			bean.setPname(fields[1]);
			bean.setFlag("1");

			k.set(fields[0]);
		}
		
		context.write(k, bean);
	}

}
